//
//  MHAlertTokenController.m
//  M-Saler
//
//  Created by Teresa on 2020/5/21.
//  Copyright © 2020 Teresa. All rights reserved.
//

#import "MHAlertTokenController.h"

@interface MHAlertTokenController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet ZKPropertyButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;

@end

@implementation MHAlertTokenController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnce:)];
    tap.delegate = self;
    tap.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tap];
}

- (IBAction)closeBtn:(id)sender {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (IBAction)sharedToWechat:(id)sender {
    
    NSString *string = [NSString stringWithFormat:@"漫时光推销员分享给你的口令:%@",self.codeLabel.text];
    [self closeBtn:nil];
    if (self.copyBlock) {
        self.copyBlock(string);
    }
    
//    NSURL *url = [NSURL URLWithString:@"weixin://"];
//    BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
//    if (canOpen){   //打开微信
//        [[UIApplication sharedApplication] openURL:url];
//        //[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
//    }else {
//        [[HUDHelper sharedInstance]tipMessage:@"您的设备未安装微信APP" delay:1];
//    }
}

-(void)tapOnce:(UITapGestureRecognizer*)gesture{
    [self closeBtn:nil];
}

-(void)showToken:(NSString *)token inController:(UIViewController *)vc able2Share:(BOOL)isAble2Share{
    self.codeLabel.text = token;
    self.view.center = vc.view.center;
    self.shareBtn.hidden = !isAble2Share;
    [vc addChildViewController:self];
    [vc.view addSubview:self.view];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    return YES;
}


@end
